import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

import 'constants/constant_string.dart';
import '../resources/resources.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2023年12月27日 10:22
 */

class UpdateDialog extends Dialog {
  final String upDateContent;
  final String version;
  final bool isForce;

  const UpdateDialog(
      {super.key,
      required this.upDateContent,
      required this.version,
      required this.isForce});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(
            width: 331.h,
            height: 402.h,
            child: Stack(
              children: <Widget>[
                Image.asset(
                  ImageAssets.appUpdateBg,
                  fit: BoxFit.fill,
                  width: 331.w,
                  height: 402.h,
                ),
                SizedBox(
                  // width: double.infinity,
                  child: Column(
                    // mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(left: 34.w, top: 70.h),
                        child: Text(ResKey.upgradeNewVersionDiscover.tr,
                            style: ytTextStyle(
                                fontSize: 22,
                                color: Colors.white,
                                fontWeight: FontWeight.bold)),
                      ),
                      Container(
                          margin: EdgeInsets.only(left: 34.w, top: 5.h),
                          child: Text('V$version',
                              style: ytTextStyle(
                                  fontSize: 16,
                                  color: Colors.white,
                                  fontWeight: FontWeight.w500))),
                      Container(
                          margin: EdgeInsets.only(left: 34.w, top: 55.h),
                          child: Text(upDateContent,
                              style: ytTextStyle(
                                  fontSize: 14,
                                  // color: Colors.white,
                                  fontWeight: FontWeight.normal,
                                  height: 2.2))),
                      Spacer(),
                      Row(
                        children: [
                          SizedBox(
                            width: 24.w,
                          ),
                          Expanded(
                              child: GestureDetector(
                                  onTap: () {
                                    goToUpdate();
                                  },
                                  child: Container(
                                    height: 48.h,
                                    margin: EdgeInsets.only(
                                        left: 10.w, right: 10.w),
                                    padding: EdgeInsets.only(top: 3),
                                    alignment: Alignment.center,
                                    decoration: const BoxDecoration(
                                        color: Colour.primaryColor,
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(8))),
                                    child: Text(
                                      ResKey.upgradeUpdradeNow.tr,
                                      style: ytTextStyle(
                                          fontSize: 16,
                                          color: Colors.white,
                                          fontWeight: FontWeight.bold),
                                    ),
                                  ))),
                          SizedBox(
                            width: 24.w,
                          ),
                        ],
                      ),
                      Container(
                          // height: 36.h,
                          alignment: Alignment.center,
                          margin:  EdgeInsets.only( right: 16.w,left: 16.w,bottom: 5,top: 5),
                          child: Text(ResKey.upgradeUpdradeTimeTip.tr,
                              maxLines: 2,
                              textAlign: TextAlign.center,
                              style: ytTextStyle(
                                  fontSize: 14,

                                  color: const Color(0xFFABABAB),
                                  fontWeight: FontWeight.normal,
                                  height: 1.5))),
                      SizedBox(
                        height: 22.h,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          GestureDetector(
              onTap: () {
                isForce?null:Navigator.pop(context);
              },
              child: Offstage(
                offstage: isForce,
                child: Container(
                  margin: EdgeInsets.only(top: 24.h),
                  child: Image.asset(
                    ImageAssets.appUpdateClose,
                    fit: BoxFit.fill,
                    width: 34,
                    height: 34,
                  ),
                ),
              ))
        ],
      ),
    );
  }

  static showUpdateDialog(BuildContext context, String mUpdateContent,
      String version, bool isForce) {
    return showDialog(
        barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return WillPopScope(
              onWillPop: _onWillPop,
              child: UpdateDialog(
                  upDateContent: mUpdateContent,
                  version: version,
                  isForce: isForce));
        });
  }

  static Future<bool> _onWillPop() async {
    return false;
  }

  ///去更新
  Future<void> goToUpdate() async {
    ///iOS直接打开App Store
    if (Platform.isIOS) {
      launchUrlString(
        "itms-apps://itunes.apple.com/app/${ConstantString.appId}",
      );

      ///Android打开浏览器下载
    } else if (Platform.isAndroid) {
      await launchUrlString(ConstantString.androidDownloadUrl,
          mode: LaunchMode.externalApplication);
    }
  }
}
